package com.wanmait.wanmarecruit.webController;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.wanmait.wanmarecruit.entity.*;
import com.wanmait.wanmarecruit.service.*;
import org.apache.commons.io.FilenameUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("web")
public class WebCompanyProfileController {
    @Resource
    private CompanyService companyService;
    @Resource
    private ProvinceService provinceService;
    @Resource
    private CityService cityService;
    @Resource
    private DistrictService districtService;
    @Resource
    private CompanyPicService companyPicService;


    @RequestMapping("/companies/companyProfile")
    public ModelAndView companyProfile(HttpSession session)
    {
        Integer companyId = ((Recruiter)session.getAttribute("hr")).getCompanyId();
        Company company = companyService.getById(companyId);
        List<Province> provinces = provinceService.getProvincesBySort();
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("company",company);
        modelAndView.addObject("provinces",provinces);
        modelAndView.addObject("companyPics",companyPicService.getCompanyPicByCompanyId(companyId));
        return modelAndView;
    }

    @PostMapping("changeCityByProvId")
    @ResponseBody
    public List<City> changeCityByProvId(Integer provId)
    {
        return cityService.getCityByProvince(provId);
    }

    @PostMapping("changeDistrictByCityId")
    @ResponseBody
    public List<District> changeDistrictByCityId(Integer cityId)
    {
        return districtService.getDistrictByCity(cityId);
    }

    @PostMapping("/companies/saveCompanyInfo")
    public String saveCompanyInfo(Company company, MultipartFile facePic, MultipartFile logoPic, HttpServletRequest request,HttpSession session,MultipartFile[] pics)
    {
//        Integer companyId = ((Company)session.getAttribute("company")).getId();
        Integer companyId = 1;
        if (facePic!=null)
        {
            if (!facePic.isEmpty())
            {
                String exe = FilenameUtils.getExtension(facePic.getOriginalFilename());
                String face = UUID.randomUUID().toString()+"."+exe;
                String path = request.getServletContext().getRealPath("/static/uploadImages");
                try {
                    facePic.transferTo(new File(path+"/"+face));
                } catch (IOException e) {
                    e.printStackTrace();
                }
                company.setFace(face);
            }
        }

        if (logoPic!=null)
        {
            if (!logoPic.isEmpty())
            {
                String exe = FilenameUtils.getExtension(logoPic.getOriginalFilename());
                String logo = UUID.randomUUID().toString()+"."+exe;
                String path = request.getServletContext().getRealPath("/static/uploadImages");
                try {
                    logoPic.transferTo(new File(path+"/"+logo));
                } catch (IOException e) {
                    e.printStackTrace();
                }
                company.setCompanyLogo(logo);
            }
        }
        company.setId(companyId);
        companyService.updateById(company);
        List<CompanyPic> companyPics = companyPicService.getCompanyPicByCompanyId(companyId);
        String saveOrUpdate= "save";
        if (companyPics.size()>=5)
        {
            saveOrUpdate="update";
        }
        for (int i = 0; i < pics.length; i++) {
            if (!pics[i].isEmpty()) {
                String exe = FilenameUtils.getExtension(pics[i].getOriginalFilename());
                String face = UUID.randomUUID().toString() + "." + exe;
                String path = request.getServletContext().getRealPath("/static/uploadImages");
                try {
                    pics[i].transferTo(new File(path + "/" + face));
                } catch (IOException e) {
                    e.printStackTrace();
                }
                if (saveOrUpdate.equals("update"))
                {
                    CompanyPic companyPic = companyPics.get(i);
                    companyPic.setPic(face);
                }
                else
                {
                    CompanyPic companyPic = new CompanyPic();
                    companyPic.setPic(face);
                    companyPic.setCompanyId(companyId);
                    companyPics.add(companyPic);
                }
            }
        }
        companyPicService.saveOrUpdateBatch(companyPics);
        return "redirect:/web/companies/companyProfile?success=1";
    }



}
